private byte SelectUncompressedPresentationContext(ClientAssociationParameters association, DicomMessage msg) { byte pcid = association.FindAbstractSyntaxWithTransferSyntax(msg.SopClass, TransferSyntax.ExplicitVrLittleEndian); if (pcid == 0) { pcid = association.FindAbstractSyntaxWithTransferSyntax(msg.SopClass, TransferSyntax.ImplicitVrLittleEndian); } return(pcid); }
/// <summary> /// Generic routine to send the next C-STORE-RQ message in the _fileList. /// </summary> /// <param name="client">DICOM Client class</param> /// <param name="association">Association Parameters</param> public bool SendCStore(DicomClient client, ClientAssociationParameters association) { FileToSend fileToSend = _fileList[_fileListIndex]; DicomFile dicomFile = new DicomFile(fileToSend.filename); try { dicomFile.Load(DicomReadOptions.Default); } catch (DicomException e) { Logger.LogErrorException(e, "Unexpected exception when loading DICOM file {0}", fileToSend.filename); return(false); } DicomMessage msg = new DicomMessage(dicomFile); byte pcid = association.FindAbstractSyntaxWithTransferSyntax(fileToSend.sopClass, dicomFile.TransferSyntax); if (pcid == 0) { Logger.LogError( "Unable to find matching negotiated presentation context for sop {0} and syntax {1}", dicomFile.SopClass.Name, dicomFile.TransferSyntax.Name); return(false); } client.SendCStoreRequest(pcid, client.NextMessageID(), DicomPriority.Medium, msg); return(true); }
/// <summary> /// Scan the files to send, and create presentation contexts for each abstract syntax to send. /// </summary> public void SetPresentationContexts() { foreach (FileToSend sendStruct in _fileList) { byte pcid = _assocParams.FindAbstractSyntaxWithTransferSyntax(sendStruct.sopClass, sendStruct.transferSyntax); if (pcid == 0) { pcid = _assocParams.AddPresentationContext(sendStruct.sopClass); _assocParams.AddTransferSyntax(pcid, sendStruct.transferSyntax); } } }
/// <summary> /// Called when received associate accept. For StorageScu, we then attempt to send the first file. /// </summary> /// <param name="client">The client.</param> /// <param name="association">The association.</param> public override void OnReceiveAssociateAccept(DicomClient client, ClientAssociationParameters association) { base.OnReceiveAssociateAccept(client, association); LogAdapter.Logger.Info("Association Accepted:\r\n{0}", association.ToString()); byte pcid = association.FindAbstractSyntaxWithTransferSyntax(SopClass.StorageCommitmentPushModelSopClass, TransferSyntax.ExplicitVrLittleEndian); if (pcid == 0) { pcid = association.FindAbstractSyntaxWithTransferSyntax(SopClass.StorageCommitmentPushModelSopClass, TransferSyntax.ImplicitVrLittleEndian); } if (pcid == 0) { client.SendAssociateAbort(DicomAbortSource.ServiceUser, DicomAbortReason.NotSpecified); return; } DicomMessage msg = new DicomMessage(); msg.RequestedSopInstanceUid = "1.2.840.10008.1.20.1.1"; msg.ActionTypeId = 1; msg.DataSet[DicomTags.TransactionUid].SetStringValue(DicomUid.GenerateUid().UID); foreach (StorageInstance instance in StorageInstanceList) { DicomSequenceItem item = new DicomSequenceItem(); msg.DataSet[DicomTags.ReferencedSopSequence].AddSequenceItem(item); item[DicomTags.ReferencedSopClassUid].SetStringValue(instance.SopClass.Uid); item[DicomTags.ReferencedSopInstanceUid].SetStringValue(instance.SopInstanceUid); } }
private byte SelectPresentationContext(ClientAssociationParameters association, StorageInstance fileToSend, out DicomMessage msg) { byte pcid; if (PresentationContextSelectionDelegate != null) { // Note, this may do a conversion of the file according to codecs, need to catch a codec exception if it occurs var dicomFile = fileToSend.LoadFile(); pcid = PresentationContextSelectionDelegate(association, dicomFile, out msg); } else { msg = null; pcid = association.FindAbstractSyntaxWithTransferSyntax(fileToSend.SopClass, fileToSend.TransferSyntax); if (fileToSend.TransferSyntax.Encapsulated) { if (pcid == 0) { // We can compress/decompress the file. Check if remote device accepts it if (DicomCodecRegistry.GetCodec(fileToSend.TransferSyntax) != null) { var dicomFile = fileToSend.LoadFile(); msg = new DicomMessage(dicomFile); pcid = SelectUncompressedPresentationContext(association, msg); } } } else { if (pcid == 0) { var dicomFile = fileToSend.LoadFile(); msg = new DicomMessage(dicomFile); pcid = SelectUncompressedPresentationContext(association, msg); } } if (pcid != 0 && fileToSend.FileIsLoaded) { msg = new DicomMessage(fileToSend.LoadFile()); } } return(pcid); }
public void OnReceiveAssociateAccept(DicomClient client, ClientAssociationParameters association) { if (_type == TestTypes.AssociationReject) { Assert.Fail("Unexpected negotiated association on reject test."); } else if (_type == TestTypes.SendMR) { DicomMessage msg = new DicomMessage(); _test.SetupMR(msg.DataSet); byte id = association.FindAbstractSyntaxWithTransferSyntax(msg.SopClass, TransferSyntax.ExplicitVrLittleEndian); client.SendCStoreRequest(id, client.NextMessageID(), DicomPriority.Medium, msg); } else { Assert.Fail("Unexpected test type"); } }
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); }
/// <summary> /// Called when received associate accept. For StorageScu, we then attempt to send the first file. /// </summary> /// <param name="client">The client.</param> /// <param name="association">The association.</param> public override void OnReceiveAssociateAccept(DicomClient client, ClientAssociationParameters association) { base.OnReceiveAssociateAccept(client, association); Platform.Log(LogLevel.Info, "Association Accepted:\r\n{0}", association.ToString()); byte pcid = association.FindAbstractSyntaxWithTransferSyntax(SopClass.StorageCommitmentPushModelSopClass, TransferSyntax.ExplicitVrLittleEndian); if (pcid == 0) pcid = association.FindAbstractSyntaxWithTransferSyntax(SopClass.StorageCommitmentPushModelSopClass, TransferSyntax.ImplicitVrLittleEndian); if (pcid == 0) { client.SendAssociateAbort(DicomAbortSource.ServiceUser, DicomAbortReason.NotSpecified); return; } DicomMessage msg = new DicomMessage(); msg.RequestedSopInstanceUid = "1.2.840.10008.1.20.1.1"; msg.ActionTypeId = 1; msg.DataSet[DicomTags.TransactionUid].SetStringValue(DicomUid.GenerateUid().UID); foreach (StorageInstance instance in StorageInstanceList) { DicomSequenceItem item = new DicomSequenceItem(); msg.DataSet[DicomTags.ReferencedSopSequence].AddSequenceItem(item); item[DicomTags.ReferencedSopClassUid].SetStringValue(instance.SopClass.Uid); item[DicomTags.ReferencedSopInstanceUid].SetStringValue(instance.SopInstanceUid); } }
private byte SelectPresentationContext(ClientAssociationParameters association, StorageInstance fileToSend, DicomFile dicomFile, out DicomMessage msg) { byte pcid = 0; if (PresentationContextSelectionDelegate != null) { // Note, this may do a conversion of the file according to codecs, need to catch a codec exception if it occurs pcid = PresentationContextSelectionDelegate(association, dicomFile, out msg); } else { msg = new DicomMessage(dicomFile); if (fileToSend.TransferSyntax.Encapsulated) { pcid = association.FindAbstractSyntaxWithTransferSyntax(msg.SopClass, msg.TransferSyntax); if (DicomCodecRegistry.GetCodec(fileToSend.TransferSyntax) != null) { // We can compress/decompress the file. Check if remote device accepts it if (pcid == 0) pcid = SelectUncompressedPresentationContext(association, msg); } } else { if (pcid == 0) pcid = SelectUncompressedPresentationContext(association, msg); } } return pcid; }
private byte SelectUncompressedPresentationContext(ClientAssociationParameters association, DicomMessage msg) { byte pcid = association.FindAbstractSyntaxWithTransferSyntax(msg.SopClass, TransferSyntax.ExplicitVrLittleEndian); if (pcid == 0) pcid = association.FindAbstractSyntaxWithTransferSyntax(msg.SopClass, TransferSyntax.ImplicitVrLittleEndian); return pcid; }
/// <summary> /// Generic routine to send the next C-STORE-RQ message in the <see cref="StorageInstanceList"/>. /// </summary> /// <param name="client">DICOM Client class</param> /// <param name="association">Association Parameters</param> private bool SendCStore(DicomClient client, ClientAssociationParameters association) { StorageInstance fileToSend = _storageInstanceList[_fileListIndex]; OnImageStoreStarted(fileToSend); DicomFile dicomFile; try { // Check to see if image does not exist or is corrupted if (fileToSend.SendStatus == DicomStatuses.ProcessingFailure) { _failureSubOperations++; _remainingSubOperations--; OnImageStoreCompleted(fileToSend); return(false); } dicomFile = fileToSend.LoadFile(); } catch (DicomException e) { Platform.Log(LogLevel.Error, e, "Unexpected exception when loading DICOM file {0}", fileToSend.Filename); fileToSend.ExtendedFailureDescription = e.GetType().Name + " " + e.Message; _failureSubOperations++; _remainingSubOperations--; OnImageStoreCompleted(fileToSend); return(false); } DicomMessage msg = new DicomMessage(dicomFile); byte pcid = 0; if (fileToSend.TransferSyntax.Encapsulated) { pcid = association.FindAbstractSyntaxWithTransferSyntax(fileToSend.SopClass, fileToSend.TransferSyntax); if (DicomCodecRegistry.GetCodec(fileToSend.TransferSyntax) != null) { if (pcid == 0) { pcid = association.FindAbstractSyntaxWithTransferSyntax(fileToSend.SopClass, TransferSyntax.ExplicitVrLittleEndian); } if (pcid == 0) { pcid = association.FindAbstractSyntaxWithTransferSyntax(fileToSend.SopClass, TransferSyntax.ImplicitVrLittleEndian); } } } else { if (pcid == 0) { pcid = association.FindAbstractSyntaxWithTransferSyntax(fileToSend.SopClass, TransferSyntax.ExplicitVrLittleEndian); } if (pcid == 0) { pcid = association.FindAbstractSyntaxWithTransferSyntax(fileToSend.SopClass, TransferSyntax.ImplicitVrLittleEndian); } } if (pcid == 0) { fileToSend.SendStatus = DicomStatuses.SOPClassNotSupported; fileToSend.ExtendedFailureDescription = "No valid presentation contexts for file."; OnImageStoreCompleted(fileToSend); _failureSubOperations++; _remainingSubOperations--; return(false); } try { if (_moveOriginatorAe == null) { client.SendCStoreRequest(pcid, client.NextMessageID(), DicomPriority.Medium, msg); } else { client.SendCStoreRequest(pcid, client.NextMessageID(), DicomPriority.Medium, _moveOriginatorAe, _moveOriginatorMessageId, msg); } } catch (DicomNetworkException) { throw; //This is a DicomException-derived class that we want to throw. } catch (DicomCodecException e) { Platform.Log(LogLevel.Error, e, "Unexpected exception when compressing or decompressing file before send {0}", fileToSend.Filename); fileToSend.SendStatus = DicomStatuses.ProcessingFailure; fileToSend.ExtendedFailureDescription = "Error decompressing or compressing file before send."; OnImageStoreCompleted(fileToSend); _failureSubOperations++; _remainingSubOperations--; return(false); } catch (DicomException e) { Platform.Log(LogLevel.Error, e, "Unexpected exception while sending file {0}", fileToSend.Filename); fileToSend.SendStatus = DicomStatuses.ProcessingFailure; fileToSend.ExtendedFailureDescription = "Unexpected exception while sending file."; OnImageStoreCompleted(fileToSend); _failureSubOperations++; _remainingSubOperations--; return(false); } return(true); }
/// <summary> /// Generic routine to send the next C-STORE-RQ message in the <see cref="StorageInstanceList"/>. /// </summary> /// <param name="client">DICOM Client class</param> /// <param name="association">Association Parameters</param> private bool SendCStore(DicomClient client, ClientAssociationParameters association) { StorageInstance fileToSend = _storageInstanceList[_fileListIndex]; OnImageStoreStarted(fileToSend); DicomFile dicomFile; try { // Check to see if image does not exist or is corrupted if (fileToSend.SendStatus == DicomStatuses.ProcessingFailure) { _failureSubOperations++; _remainingSubOperations--; OnImageStoreCompleted(fileToSend); return false; } dicomFile = fileToSend.LoadFile(); } catch (DicomException e) { Platform.Log(LogLevel.Error, e, "Unexpected exception when loading DICOM file {0}", fileToSend.Filename); fileToSend.ExtendedFailureDescription = e.GetType().Name + " " + e.Message; _failureSubOperations++; _remainingSubOperations--; OnImageStoreCompleted(fileToSend); return false; } DicomMessage msg = new DicomMessage(dicomFile); byte pcid = 0; if (fileToSend.TransferSyntax.Encapsulated) { pcid = association.FindAbstractSyntaxWithTransferSyntax(fileToSend.SopClass, fileToSend.TransferSyntax); if (DicomCodecRegistry.GetCodec(fileToSend.TransferSyntax) != null) { if (pcid == 0) pcid = association.FindAbstractSyntaxWithTransferSyntax(fileToSend.SopClass, TransferSyntax.ExplicitVrLittleEndian); if (pcid == 0) pcid = association.FindAbstractSyntaxWithTransferSyntax(fileToSend.SopClass, TransferSyntax.ImplicitVrLittleEndian); } } else { if (pcid == 0) pcid = association.FindAbstractSyntaxWithTransferSyntax(fileToSend.SopClass, TransferSyntax.ExplicitVrLittleEndian); if (pcid == 0) pcid = association.FindAbstractSyntaxWithTransferSyntax(fileToSend.SopClass, TransferSyntax.ImplicitVrLittleEndian); } if (pcid == 0) { fileToSend.SendStatus = DicomStatuses.SOPClassNotSupported; fileToSend.ExtendedFailureDescription = "No valid presentation contexts for file."; OnImageStoreCompleted(fileToSend); _failureSubOperations++; _remainingSubOperations--; return false; } try { if (_moveOriginatorAe == null) client.SendCStoreRequest(pcid, client.NextMessageID(), DicomPriority.Medium, msg); else client.SendCStoreRequest(pcid, client.NextMessageID(), DicomPriority.Medium, _moveOriginatorAe, _moveOriginatorMessageId, msg); } catch(DicomNetworkException) { throw; //This is a DicomException-derived class that we want to throw. } catch(DicomCodecException e) { Platform.Log(LogLevel.Error, e, "Unexpected exception when compressing or decompressing file before send {0}", fileToSend.Filename); fileToSend.SendStatus = DicomStatuses.ProcessingFailure; fileToSend.ExtendedFailureDescription = "Error decompressing or compressing file before send."; OnImageStoreCompleted(fileToSend); _failureSubOperations++; _remainingSubOperations--; return false; } catch(DicomException e) { Platform.Log(LogLevel.Error, e, "Unexpected exception while sending file {0}", fileToSend.Filename); fileToSend.SendStatus = DicomStatuses.ProcessingFailure; fileToSend.ExtendedFailureDescription = "Unexpected exception while sending file."; OnImageStoreCompleted(fileToSend); _failureSubOperations++; _remainingSubOperations--; return false; } return true; }