Exemplo n.º 1
0
 /// <summary>
 ///     Creates a new VR instance from a DICOM output stream.
 /// </summary>
 /// <param name="stream">
 ///     Any kind of DICOM output stream.
 /// </param>
 /// <returns>
 ///     Output stream position of this instance.
 /// </returns>
 public static ValueRepresentation LoadFrom(Stream stream, Tag tag)
 {
     DicomContext.Set(stream, tag);
     if (IsImplicitBy(tag))
     {
         if (tag.IsUserDefined)
         {
             // implicit but unknown value representation
             return(GetBy("UN", tag));
         }
         else
         {
             // implicit but known value representation;
             // return new instance, dictionary entry do not have a
             // transfer syntax
             return(GetBy(tag.GetDictionaryEntry().VR.Name, tag));
         }
     }
     else
     {
         // explicit value representation
         byte[] buffer = new byte[2];
         stream.Read(buffer, 0, 2);
         string name = ByteConvert.ToString(buffer,
                                            CharacterRepertoire.Default);
         return(GetBy(name, tag));
     }
     DicomContext.Reset();
 }
        private void LoadFromBinary(StreamReader streamReader)
        {
            BinaryReader binaryReader = new BinaryReader(streamReader.BaseStream);

            while (streamReader.BaseStream.Position < streamReader.BaseStream.Length)
            {
                try
                {
                    int    length = binaryReader.ReadInt32();
                    byte[] buffer = new byte[length];
                    binaryReader.Read(buffer, 0, length);
                    string stringUid = ByteConvert.ToString(buffer,
                                                            CharacterRepertoire.Ascii);
                    Uid uid = new Uid(stringUid);
                    length = binaryReader.ReadInt32();
                    buffer = new byte[length];
                    binaryReader.Read(buffer, 0, length);
                    string name = ByteConvert.ToString(buffer,
                                                       CharacterRepertoire.Ascii);
                    int     intType = binaryReader.ReadInt32();
                    UidType type    = (UidType)UidType.ToObject(typeof(UidType),
                                                                intType);
                    bool retired             = binaryReader.ReadBoolean();
                    UidDictionaryEntry entry =
                        new UidDictionaryEntry(uid, name, type, retired);
                    Add(entry);
                }
                catch (Exception e)
                {
                    throw new DicomException("Wrong entry before index " +
                                             streamReader.BaseStream.Position + ": " + e.Message);
                }
            }
        }
Exemplo n.º 3
0
 /// <summary>
 ///     Re-creates this instance from a DICOM output stream.
 /// </summary>
 public void LoadFrom(Stream stream)
 {
     streamPosition = stream.Position;
     DicomContext.Set(stream, tag);
     if (IsImplicit)
     {
         if (Tag.IsUserDefined)
         {
             // implicit but unknown value representation
             vrName = "UN";
         }
         else
         {
             // implicit but known value representation;
             // return new instance, dictionary entry do not have a
             // transfer syntax
             vrName = Tag.GetDictionaryEntry().VR.Name;
         }
     }
     else
     {
         // explicit value representation
         byte[] buffer = new byte[2];
         stream.Read(buffer, 0, 2);
         vrName = ByteConvert.ToString(buffer,
                                       CharacterRepertoire.Default);
     }
     DicomContext.Reset();
 }
        private void LoadFromBinary(StreamReader streamReader)
        {
            BinaryReader binaryReader = new BinaryReader(streamReader.BaseStream);

            while (streamReader.BaseStream.Position < streamReader.BaseStream.Length)
            {
                try
                {
                    int    group   = binaryReader.ReadInt32();
                    int    element = binaryReader.ReadInt32();
                    Tag    tag     = new Tag(group, element);
                    int    length  = binaryReader.ReadInt32();
                    byte[] buffer  = new byte[length];
                    binaryReader.Read(buffer, 0, length);
                    string description = ByteConvert.ToString(buffer,
                                                              CharacterRepertoire.Ascii);
                    buffer = new byte[2];
                    binaryReader.Read(buffer, 0, 2);
                    string vr = ByteConvert.ToString(buffer,
                                                     CharacterRepertoire.Ascii);
                    vr     = vr.Trim();
                    length = binaryReader.ReadInt32();
                    buffer = new byte[length];
                    binaryReader.Read(buffer, 0, length);
                    string vm = ByteConvert.ToString(buffer,
                                                     CharacterRepertoire.Ascii);
                    bool   retiredBool = binaryReader.ReadBoolean();
                    string retired     = retiredBool ? "RET" : null;
                    DataElementDictionaryEntry entry =
                        new DataElementDictionaryEntry(tag.ToString(),
                                                       description, vr, vm, retired);
                    Add(entry);
                }
                catch (Exception e)
                {
                    throw new DicomException("Wrong entry before index " +
                                             streamReader.BaseStream.Position + ": " + e.Message);
                }
            }
        }