Exemple #1
0
        /// <summary>
        /// Method for dumping the contents of the element to a string.
        /// </summary>
        /// <param name="sb">The StringBuilder to write the element to.</param>
        /// <param name="prefix">A prefix to place before the value.</param>
        /// <param name="options">The <see cref="DicomDumpOptions"/> to use for the output string.</param>
        public virtual void Dump(StringBuilder sb, string prefix, DicomDumpOptions options)
        {
            int ValueWidth = 40 - prefix.Length;
            int SbLength   = sb.Length;

            sb.Append(prefix);
            sb.AppendFormat("({0:x4},{1:x4}) {2} ", Tag.Group, Tag.Element, Tag.VR.Name);
            if (Count == 0)
            {
                String value = "(no value available)";
                sb.Append(value.PadRight(ValueWidth, ' '));
            }
            else
            {
                if (Tag.VR.IsTextVR)
                {
                    String value;
                    if (Tag.VR == DicomVr.UIvr)
                    {
                        DicomElementUi ui = this as DicomElementUi;

                        DicomUid uid;
                        bool     ok = ui.TryGetUid(0, out uid);

                        if (ok && uid.Type != UidType.Unknown)
                        {
                            value = "=" + uid.Description;
                            if (Flags.IsSet(options, DicomDumpOptions.ShortenLongValues))
                            {
                                if (value.Length > ValueWidth)
                                {
                                    value = value.Substring(0, ValueWidth - 3);
                                }
                            }
                        }
                        else
                        {
                            value = "[" + ToString() + "]";
                            if (Flags.IsSet(options, DicomDumpOptions.ShortenLongValues))
                            {
                                if (value.Length > ValueWidth)
                                {
                                    value = value.Substring(0, ValueWidth - 4) + "...]";
                                }
                            }
                        }
                    }
                    else
                    {
                        value = "[" + ToString() + "]";
                        if (Flags.IsSet(options, DicomDumpOptions.ShortenLongValues))
                        {
                            if (value.Length > ValueWidth)
                            {
                                value = value.Substring(0, ValueWidth - 4) + "...]";
                            }
                        }
                    }
                    sb.Append(value.PadRight(ValueWidth, ' '));
                }
                else
                {
                    String value = ToString();
                    if (Flags.IsSet(options, DicomDumpOptions.ShortenLongValues))
                    {
                        if (value.Length > ValueWidth)
                        {
                            value = value.Substring(0, ValueWidth - 3) + "...";
                        }
                    }
                    sb.Append(value.PadRight(ValueWidth, ' '));
                }
            }
            sb.AppendFormat(" # {0,4} {2} {1}", StreamLength, Tag.VM, Tag.Name.Replace("&apos;", "'"));

            if (Flags.IsSet(options, DicomDumpOptions.Restrict80CharactersPerLine))
            {
                if (sb.Length > (SbLength + 79))
                {
                    sb.Length = SbLength + 79;
                    //sb.Append(">");
                }
            }
        }
Exemple #2
0
        private void LoadCore(Stream stream, DicomStreamOpener streamOpener, DicomTag stopTag, DicomReadOptions options)
        {
            // TODO CR (24 Jan 2014): DICOM stream read only uses tag value, so the real implementation should be the uint overload!
            if (stopTag == null)
            {
                stopTag = new DicomTag(0xFFFFFFFF, "Bogus Tag", "BogusTag", DicomVr.NONE, false, 1, 1, false);
            }

            DicomStreamReader dsr;

            var iStream = stream ?? streamOpener.Open();

            if (iStream.CanSeek)
            {
                iStream.Seek(128, SeekOrigin.Begin);
                if (!FileHasPart10Header(iStream))
                {
                    if (!Flags.IsSet(options, DicomReadOptions.ReadNonPart10Files))
                    {
                        throw new DicomException(String.Format("File is not part 10 format file: {0}", Filename));
                    }

                    iStream.Seek(0, SeekOrigin.Begin);
                    dsr = new DicomStreamReader(iStream)
                    {
                        StreamOpener   = streamOpener,
                        TransferSyntax = TransferSyntax.ImplicitVrLittleEndian,
                        Dataset        = DataSet
                    };
                    DicomReadStatus stat = dsr.Read(stopTag, options);
                    if (stat != DicomReadStatus.Success)
                    {
                        LogAdapter.Logger.Error("Unexpected error when reading file: {0}", Filename);
                        throw new DicomException("Unexpected read error with file: " + Filename);
                    }

                    TransferSyntax = TransferSyntax.ImplicitVrLittleEndian;
                    if (DataSet.Contains(DicomTags.SopClassUid))
                    {
                        MediaStorageSopClassUid = DataSet[DicomTags.SopClassUid].ToString();
                    }
                    if (DataSet.Contains(DicomTags.SopInstanceUid))
                    {
                        MediaStorageSopInstanceUid = DataSet[DicomTags.SopInstanceUid].ToString();
                    }

                    Loaded = true;
                    return;
                }
            }
            else
            {
                // TODO CR (04 Apr 2014): this code here is almost identical to the seekable stream above, except that we use the 4CC wrapper
                // we can combine these two when we trust that the wrapper works in all cases
                iStream = FourCcReadStream.Create(iStream);

                // Read the 128 byte header first, then check for DICM
                iStream.SeekEx(128, SeekOrigin.Begin);

                if (!FileHasPart10Header(iStream))
                {
                    if (!Flags.IsSet(options, DicomReadOptions.ReadNonPart10Files))
                    {
                        throw new DicomException(String.Format("File is not part 10 format file: {0}", Filename));
                    }

                    iStream.Seek(0, SeekOrigin.Begin);
                    dsr = new DicomStreamReader(iStream)
                    {
                        StreamOpener   = streamOpener,
                        TransferSyntax = TransferSyntax.ImplicitVrLittleEndian,
                        Dataset        = DataSet
                    };
                    DicomReadStatus stat = dsr.Read(stopTag, options);
                    if (stat != DicomReadStatus.Success)
                    {
                        LogAdapter.Logger.Error("Unexpected error when reading file: {0}", Filename);
                        throw new DicomException("Unexpected read error with file: " + Filename);
                    }

                    TransferSyntax = TransferSyntax.ImplicitVrLittleEndian;
                    if (DataSet.Contains(DicomTags.SopClassUid))
                    {
                        MediaStorageSopClassUid = DataSet[DicomTags.SopClassUid].ToString();
                    }
                    if (DataSet.Contains(DicomTags.SopInstanceUid))
                    {
                        MediaStorageSopInstanceUid = DataSet[DicomTags.SopInstanceUid].ToString();
                    }

                    Loaded = true;
                    return;
                }
            }

            dsr = new DicomStreamReader(iStream)
            {
                TransferSyntax = TransferSyntax.ExplicitVrLittleEndian,
                StreamOpener   = streamOpener,
                Dataset        = MetaInfo
            };

            DicomReadStatus readStat =
                dsr.Read(new DicomTag(0x0002FFFF, "Bogus Tag", "BogusTag", DicomVr.UNvr, false, 1, 1, false), options);

            if (readStat != DicomReadStatus.Success)
            {
                LogAdapter.Logger.Error("Unexpected error when reading file Meta info for file: {0}", Filename);
                throw new DicomException("Unexpected failure reading file Meta info for file: " + Filename);
            }
            MetaInfoFileLength = dsr.EndGroupTwo + 128 + 4;

            dsr.Dataset        = DataSet;
            dsr.TransferSyntax = TransferSyntax;
            readStat           = dsr.Read(stopTag, options);
            if (readStat != DicomReadStatus.Success)
            {
                LogAdapter.Logger.Error("Unexpected error ({0}) when reading file at offset {2}: {1}", readStat, Filename, dsr.BytesRead);
                throw new DicomException("Unexpected failure (" + readStat + ") reading file at offset " + dsr.BytesRead + ": " + Filename);
            }

            Loaded = true;
        }