예제 #1
0
        public DicomCompressCommand(DicomMessageBase file, XmlDocument parms, bool failOnCodecException)
            : base("DICOM Compress Command", true)
        {
            _file = file;
            _failOnCodecException = failOnCodecException;

            XmlElement element = parms.DocumentElement;

            string syntax = element.Attributes["syntax"].Value;

            _syntax = TransferSyntax.GetTransferSyntax(syntax);
            if (_syntax == null)
            {
                string failureDescription =
                    String.Format("Invalid transfer syntax in compression command: {0}", element.Attributes["syntax"].Value);
                Platform.Log(LogLevel.Error, "Error with input syntax: {0}", failureDescription);
                throw new DicomCodecException(failureDescription);
            }

            IDicomCodecFactory[] codecs          = DicomCodecRegistry.GetCodecFactories();
            IDicomCodecFactory   theCodecFactory = null;

            foreach (IDicomCodecFactory codec in codecs)
            {
                if (codec.CodecTransferSyntax.Equals(_syntax))
                {
                    theCodecFactory = codec;
                    break;
                }
            }

            if (theCodecFactory == null)
            {
                string failureDescription = String.Format("Unable to find codec for compression: {0}", _syntax.Name);
                Platform.Log(LogLevel.Error, "Error with compression input parameters: {0}", failureDescription);
                throw new DicomCodecException(failureDescription);
            }

            _codec = theCodecFactory.GetDicomCodec();
            _parms = theCodecFactory.GetCodecParameters(parms);
        }
        private byte SelectPresentationContext(ClientAssociationParameters association, DicomFile file, out DicomMessage message)
        {
            byte pcid = 0;

            message = new DicomMessage(file);

            // If Lossy compressed & we have a matching context, send
            // If we don't have a codec, just return
            if (message.TransferSyntax.Encapsulated && message.TransferSyntax.LossyCompressed)
            {
                pcid = association.FindAbstractSyntaxWithTransferSyntax(message.SopClass, message.TransferSyntax);
                if (pcid != 0)
                {
                    return(pcid);
                }

                if (DicomCodecRegistry.GetCodec(message.TransferSyntax) == null)
                {
                    return(0);
                }
            }

            // If the image is lossless compressed & we don't have a codec, send if we
            // can as is.
            if (message.TransferSyntax.Encapsulated && message.TransferSyntax.LosslessCompressed)
            {
                if (DicomCodecRegistry.GetCodec(message.TransferSyntax) == null)
                {
                    pcid = association.FindAbstractSyntaxWithTransferSyntax(message.SopClass, message.TransferSyntax);
                    return(pcid);
                }
            }

            // If lossless compressed & requesting lossless syntax, just send as is
            if (message.TransferSyntax.Encapsulated &&
                message.TransferSyntax.LosslessCompressed &&
                ((_sendRequest.CompressionType == CompressionType.Rle ||
                  _sendRequest.CompressionType == CompressionType.JpegLossless ||
                  _sendRequest.CompressionType == CompressionType.J2KLossless)))
            {
                pcid = association.FindAbstractSyntaxWithTransferSyntax(message.SopClass, message.TransferSyntax);
                if (pcid != 0)
                {
                    return(pcid);
                }
            }


            if (_sendRequest.CompressionType == CompressionType.Rle)
            {
                pcid = association.FindAbstractSyntaxWithTransferSyntax(message.SopClass, TransferSyntax.RleLossless);
                if (pcid != 0)
                {
                    return(pcid);
                }
            }
            else if (_sendRequest.CompressionType == CompressionType.JpegLossless)
            {
                pcid = association.FindAbstractSyntaxWithTransferSyntax(message.SopClass, TransferSyntax.JpegLosslessNonHierarchicalFirstOrderPredictionProcess14SelectionValue1);
                if (pcid != 0)
                {
                    return(pcid);
                }
            }
            else if (_sendRequest.CompressionType == CompressionType.J2KLossless)
            {
                pcid = association.FindAbstractSyntaxWithTransferSyntax(message.SopClass, TransferSyntax.Jpeg2000ImageCompressionLosslessOnly);
                if (pcid != 0)
                {
                    return(pcid);
                }
            }
            else if (_sendRequest.CompressionType == CompressionType.J2KLossy)
            {
                pcid = association.FindAbstractSyntaxWithTransferSyntax(message.SopClass, TransferSyntax.Jpeg2000ImageCompression);
                if (pcid != 0)
                {
                    var doc = new XmlDocument();

                    XmlElement element = doc.CreateElement("compress");
                    doc.AppendChild(element);
                    XmlAttribute syntaxAttribute = doc.CreateAttribute("syntax");
                    syntaxAttribute.Value = TransferSyntax.Jpeg2000ImageCompressionUid;
                    element.Attributes.Append(syntaxAttribute);

                    decimal      ratio          = 100.0m / _sendRequest.CompressionLevel;
                    XmlAttribute ratioAttribute = doc.CreateAttribute("ratio");
                    ratioAttribute.Value = ratio.ToString(CultureInfo.InvariantCulture);
                    element.Attributes.Append(ratioAttribute);

                    syntaxAttribute       = doc.CreateAttribute("convertFromPalette");
                    syntaxAttribute.Value = true.ToString(CultureInfo.InvariantCulture);
                    element.Attributes.Append(syntaxAttribute);

                    IDicomCodecFactory[] codecs = DicomCodecRegistry.GetCodecFactories();
                    foreach (IDicomCodecFactory codec in codecs)
                    {
                        if (codec.CodecTransferSyntax.Equals(TransferSyntax.Jpeg2000ImageCompression))
                        {
                            try
                            {
                                if (message.TransferSyntax.Encapsulated)
                                {
                                    message.ChangeTransferSyntax(TransferSyntax.ExplicitVrLittleEndian);
                                    message.TransferSyntax = TransferSyntax.ExplicitVrLittleEndian;
                                }
                                message.ChangeTransferSyntax(TransferSyntax.Jpeg2000ImageCompression,
                                                             codec.GetDicomCodec(),
                                                             codec.GetCodecParameters(doc));
                                message.TransferSyntax = TransferSyntax.Jpeg2000ImageCompression;
                                return(pcid);
                            }
                            catch (Exception e)
                            {
                                Platform.Log(LogLevel.Warn, e, "Unexpected exception changing transfer syntax to {0}.",
                                             TransferSyntax.Jpeg2000ImageCompression.Name);
                            }
                        }
                    }
                }
            }
            else if (_sendRequest.CompressionType == CompressionType.JpegLossy)
            {
                var iod = new ImagePixelMacroIod(message.DataSet);

                if (iod.BitsStored == 8)
                {
                    pcid = association.FindAbstractSyntaxWithTransferSyntax(message.SopClass,
                                                                            TransferSyntax.JpegBaselineProcess1);
                    if (pcid != 0)
                    {
                        var doc = new XmlDocument();

                        XmlElement element = doc.CreateElement("compress");
                        doc.AppendChild(element);
                        XmlAttribute syntaxAttribute = doc.CreateAttribute("syntax");
                        syntaxAttribute.Value = TransferSyntax.JpegBaselineProcess1Uid;
                        element.Attributes.Append(syntaxAttribute);

                        syntaxAttribute       = doc.CreateAttribute("quality");
                        syntaxAttribute.Value = _sendRequest.CompressionLevel.ToString(CultureInfo.InvariantCulture);
                        element.Attributes.Append(syntaxAttribute);

                        syntaxAttribute       = doc.CreateAttribute("convertFromPalette");
                        syntaxAttribute.Value = true.ToString(CultureInfo.InvariantCulture);
                        element.Attributes.Append(syntaxAttribute);

                        IDicomCodecFactory[] codecs = DicomCodecRegistry.GetCodecFactories();
                        foreach (IDicomCodecFactory codec in codecs)
                        {
                            if (codec.CodecTransferSyntax.Equals(TransferSyntax.JpegBaselineProcess1))
                            {
                                try
                                {
                                    if (message.TransferSyntax.Encapsulated)
                                    {
                                        message.ChangeTransferSyntax(TransferSyntax.ExplicitVrLittleEndian);
                                        message.TransferSyntax = TransferSyntax.ExplicitVrLittleEndian;
                                    }

                                    message.ChangeTransferSyntax(TransferSyntax.JpegBaselineProcess1,
                                                                 codec.GetDicomCodec(),
                                                                 codec.GetCodecParameters(doc));
                                    message.TransferSyntax = TransferSyntax.JpegBaselineProcess1;
                                    return(pcid);
                                }
                                catch (Exception e)
                                {
                                    Platform.Log(LogLevel.Warn, e,
                                                 "Unexpected exception changing transfer syntax to {0}.",
                                                 TransferSyntax.JpegBaselineProcess1.Name);
                                }
                            }
                        }
                    }
                }
                else if (iod.BitsStored == 12)
                {
                    pcid = association.FindAbstractSyntaxWithTransferSyntax(message.SopClass,
                                                                            TransferSyntax.JpegExtendedProcess24);
                    if (pcid != 0)
                    {
                        var doc = new XmlDocument();

                        XmlElement element = doc.CreateElement("compress");
                        doc.AppendChild(element);
                        XmlAttribute syntaxAttribute = doc.CreateAttribute("syntax");
                        syntaxAttribute.Value = TransferSyntax.JpegExtendedProcess24Uid;
                        element.Attributes.Append(syntaxAttribute);

                        syntaxAttribute       = doc.CreateAttribute("quality");
                        syntaxAttribute.Value = _sendRequest.CompressionLevel.ToString(CultureInfo.InvariantCulture);
                        element.Attributes.Append(syntaxAttribute);

                        syntaxAttribute       = doc.CreateAttribute("convertFromPalette");
                        syntaxAttribute.Value = true.ToString(CultureInfo.InvariantCulture);
                        element.Attributes.Append(syntaxAttribute);

                        IDicomCodecFactory[] codecs = DicomCodecRegistry.GetCodecFactories();
                        foreach (IDicomCodecFactory codec in codecs)
                        {
                            if (codec.CodecTransferSyntax.Equals(TransferSyntax.JpegExtendedProcess24))
                            {
                                try
                                {
                                    if (message.TransferSyntax.Encapsulated)
                                    {
                                        message.ChangeTransferSyntax(TransferSyntax.ExplicitVrLittleEndian);
                                        message.TransferSyntax = TransferSyntax.ExplicitVrLittleEndian;
                                    }

                                    message.ChangeTransferSyntax(TransferSyntax.JpegExtendedProcess24,
                                                                 codec.GetDicomCodec(),
                                                                 codec.GetCodecParameters(doc));
                                    message.TransferSyntax = TransferSyntax.JpegExtendedProcess24;
                                    return(pcid);
                                }
                                catch (Exception e)
                                {
                                    Platform.Log(LogLevel.Warn, e,
                                                 "Unexpected exception changing transfer syntax to {0}.",
                                                 TransferSyntax.JpegExtendedProcess24.Name);
                                }
                            }
                        }
                    }
                }
            }

            if (pcid == 0)
            {
                pcid = association.FindAbstractSyntaxWithTransferSyntax(message.SopClass,
                                                                        TransferSyntax.ExplicitVrLittleEndian);
            }
            if (pcid == 0)
            {
                pcid = association.FindAbstractSyntaxWithTransferSyntax(message.SopClass,
                                                                        TransferSyntax.ImplicitVrLittleEndian);
            }

            return(pcid);
        }
예제 #3
0
        protected override void ProcessItem(Model.WorkQueue item)
        {
            LoadUids(item);

            if (WorkQueueUidList.Count == 0)
            {
                // No UIDs associated with the WorkQueue item.  Set the status back to idle
                PostProcessing(item,
                               WorkQueueProcessorStatus.Idle,
                               WorkQueueProcessorDatabaseUpdate.ResetQueueState);
                return;
            }


            XmlElement element = item.Data.DocumentElement;

            string syntax = element.Attributes["syntax"].Value;

            TransferSyntax compressSyntax = TransferSyntax.GetTransferSyntax(syntax);

            if (compressSyntax == null)
            {
                item.FailureDescription =
                    String.Format("Invalid transfer syntax in compression WorkQueue item: {0}", element.Attributes["syntax"].Value);
                Platform.Log(LogLevel.Error, "Error with work queue item {0}: {1}", item.GetKey(), item.FailureDescription);
                base.PostProcessingFailure(item, WorkQueueProcessorFailureType.Fatal);
                return;
            }

            if (Study == null)
            {
                item.FailureDescription =
                    String.Format("Compression item does not have a linked Study record");
                Platform.Log(LogLevel.Error, "Error with work queue item {0}: {1}", item.GetKey(), item.FailureDescription);
                base.PostProcessingFailure(item, WorkQueueProcessorFailureType.Fatal);
                return;
            }

            Platform.Log(LogLevel.Info,
                         "Compressing study {0} for Patient {1} (PatientId:{2} A#:{3}) on partition {4} to {5}",
                         Study.StudyInstanceUid, Study.PatientsName, Study.PatientId,
                         Study.AccessionNumber, ServerPartition.Description, compressSyntax.Name);

            IDicomCodecFactory[] codecs          = DicomCodecRegistry.GetCodecFactories();
            IDicomCodecFactory   theCodecFactory = null;

            foreach (IDicomCodecFactory codec in codecs)
            {
                if (codec.CodecTransferSyntax.Equals(compressSyntax))
                {
                    theCodecFactory = codec;
                    break;
                }
            }

            if (theCodecFactory == null)
            {
                item.FailureDescription = String.Format("Unable to find codec for compression: {0}", compressSyntax.Name);
                Platform.Log(LogLevel.Error, "Error with work queue item {0}: {1}", item.GetKey(), item.FailureDescription);
                base.PostProcessingFailure(item, WorkQueueProcessorFailureType.Fatal);
                return;
            }

            if (!ProcessUidList(item, theCodecFactory))
            {
                PostProcessingFailure(item, WorkQueueProcessorFailureType.NonFatal);
            }
            else
            {
                Platform.Log(LogLevel.Info,
                             "Completed Compressing study {0} for Patient {1} (PatientId:{2} A#:{3}) on partition {4} to {5}",
                             Study.StudyInstanceUid, Study.PatientsName, Study.PatientId,
                             Study.AccessionNumber, ServerPartition.Description, compressSyntax.Name);


                if (compressSyntax.LossyCompressed)
                {
                    UpdateStudyStatus(StorageLocation, StudyStatusEnum.OnlineLossy, compressSyntax);
                }
                else
                {
                    UpdateStudyStatus(StorageLocation, StudyStatusEnum.OnlineLossless, compressSyntax);
                }

                PostProcessing(item,
                               WorkQueueProcessorStatus.Pending,
                               WorkQueueProcessorDatabaseUpdate.None);                 // batch processed, not complete
            }
        }