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();
 }
Exemplo n.º 2
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();
 }
Exemplo n.º 3
0
        /// <summary>
        ///     Re-creates this DICOM value length instance from specified DICOM
        ///     output stream using <see cref="ValueLength.TransferSyntax" />.
        /// </summary>
        public void LoadFrom(Stream stream)
        {
            streamPosition = stream.Position;
            DicomContext.Set(stream, VR.Tag);
            int  count       = 2;
            bool isCertainVR = VR.Name.Equals("OB") || VR.Name.Equals("OW") ||
                               VR.Name.Equals("OF") || VR.Name.Equals("SQ") ||
                               VR.Name.Equals("UT") || VR.Name.Equals("UN");

            if (isCertainVR && !VR.IsImplicit)
            {
                // explicit value representation for certain VRs
                byte[] reserved = new byte[2];
                stream.Read(reserved, 0, 2);
                if (TransferSyntax.CorrectByteOrdering(
                        BitConverter.ToUInt16(reserved, 0)) != 0)
                {
                    throw new ArgumentException("Reserved 2 bytes block is " +
                                                "not 0x0000.");
                }
                count = 4;
            }
            else if (VR.IsImplicit)
            {
                // implicit value representation
                count = 4;
            }
            byte[] buffer = new byte[count];
            stream.Read(buffer, 0, count);
            if (count == 2)
            {
                length = TransferSyntax.CorrectByteOrdering(
                    BitConverter.ToUInt16(buffer, 0));
            }
            else
            {
                uint len = TransferSyntax.CorrectByteOrdering(
                    BitConverter.ToUInt32(buffer, 0));
                if (len == 0xFFFFFFFF)
                {
                    // undefined length
                    length = -1;
                }
                else if (len > int.MaxValue)
                {
                    // casting problem from uint32 to int32
                    throw new DicomException("Value length is " +
                                             "too big for this implementation.", this.VR.Tag, "len",
                                             len.ToString());
                }
                else
                {
                    length = (int)len;
                }
            }
            DicomContext.Reset();
        }
Exemplo n.º 4
0
 /// <summary>
 ///     Re-creates this DICOM value instance from specified DICOM
 ///     output stream using <see cref="Value.TransferSyntax" />.
 /// </summary>
 public void LoadFrom(Stream stream)
 {
     streamPosition = stream.Position;
     DicomContext.Set(stream, VR.Tag);
     if (ValueLength.IsUndefined)
     {
         // use of delimitation (undefined length)
         if (IsSequence)
         {
             // sequence delimitation
             Sequence sq = new Sequence(stream, TransferSyntax);
             Add(sq);
         }
         else if (IsNestedDataSet)
         {
             // item delimitation
             NestedDataSet ds = new NestedDataSet(stream,
                                                  TransferSyntax);
             Add(ds);
         }
         else
         {
             throw new DicomException(
                       "Value length is undefined but value is whether " +
                       "sequence nor data set.", this.VR.Tag);
         }
     }
     else
     {
         if (stream.Position + valueLength.Value <= stream.Length)
         {
             // use of length value
             byte[] buffer = new byte[ValueLength.Value];
             stream.Read(buffer, 0, ValueLength.Value);
             Array multiValue = VR.Decode(buffer);
             Add(multiValue);
         }
         else
         {
             throw new DicomException("Value length is out of bounds.",
                                      "Value/stream.Length, Value/ValueLength.Value",
                                      stream.Length.ToString() + ", " +
                                      ValueLength.Value.ToString());
         }
     }
     DicomContext.Reset();
 }
Exemplo n.º 5
0
 /// <summary>
 ///     Re-creates this instance from a DICOM output stream.
 /// </summary>
 /// <param name="stream">
 ///     Any kind of DICOM output stream.
 /// </param>
 public void LoadFrom(Stream stream)
 {
     streamPosition = stream.Position;
     DicomContext.Set(stream, null);
     if (stream != null)
     {
         byte[] buffer = new byte[2];
         stream.Read(buffer, 0, 2);
         int group = BitConverter.ToUInt16(
             TransferSyntax.CorrectByteOrdering(buffer), 0);
         stream.Read(buffer, 0, 2);
         int element = BitConverter.ToUInt16(
             TransferSyntax.CorrectByteOrdering(buffer), 0);
         SetInt(group, element);
     }
     else
     {
         throw new DicomException("Tag.LoadFrom.Stream is null.",
                                  this);
     }
     DicomContext.Reset();
 }
Exemplo n.º 6
0
        /// <summary>
        ///     Re-creates a DICOM file meta information instance from a DICOM
        ///     output stream. The output stream position is assumed to be the
        ///     DICOM stream's start position (mostly zero).
        /// </summary>
        public override void LoadFrom(Stream stream)
        {
            base.streamPosition = stream.Position;
            DicomContext.Set(stream, null);
            byte[] buffer = new byte[132];
            stream.Read(buffer, 0, 132);
            filePreamble = TransferSyntax.ToString(buffer, 128);
            filePreamble = filePreamble.Replace("\0", null);
            string dicomPrefix = TransferSyntax.ToString(buffer, 128, 4);

            if (dicomPrefix.Equals(DicomPrefix))
            {
                // group length
                DataElement element = new DataElement(stream, TransferSyntax);
                Add(element);
                uint groupLength    = (uint)element.Value[0];
                long streamPosition = stream.Position;
                // consider omission of current stream position
                buffer = new byte[streamPosition + groupLength];
                stream.Read(buffer, (int)streamPosition, (int)groupLength);
                MemoryStream memoryStream = new MemoryStream(buffer);
                try
                {
                    memoryStream.Seek(streamPosition, 0);
                    base.LoadFrom(memoryStream);
                }
                finally
                {
                    memoryStream.Close();
                }
            }
            else
            {
                stream.Close();
                throw new DicomException("Working with a non-valid DICOM file.");
            }
            DicomContext.Reset();
        }
Exemplo n.º 7
0
 /// <summary>
 ///     Saves this instance to a DICOM input stream.
 /// </summary>
 /// <param name="stream">
 ///     Any kind of DICOM input stream.
 /// </param>
 public void SaveTo(Stream stream)
 {
     streamPosition = stream.Position;
     DicomContext.Set(stream, this);
     if (stream != null)
     {
         byte[] group = BitConverter.GetBytes(ushort.Parse(Group,
                                                           NumberStyles.HexNumber));
         byte[] element = BitConverter.GetBytes(ushort.Parse(Element,
                                                             NumberStyles.HexNumber));
         group   = TransferSyntax.CorrectByteOrdering(group);
         element = TransferSyntax.CorrectByteOrdering(element);
         byte[] buffer = new byte[4];
         Array.Copy(group, 0, buffer, 0, 2);
         Array.Copy(element, 0, buffer, 2, 2);
         stream.Write(buffer, 0, 4);
     }
     else
     {
         throw new DicomException("Tag.SaveToStream.Stream is null.",
                                  this);
     }
     DicomContext.Reset();
 }