public void Save() { if (stream != null) { stream.Close(); stream.Dispose(); stream = null; } try { // TODO if an exception is thrown, the old file is truncated dicom.Write(filename); } catch (Exception ex) { MessageBox.Show(Logging.Log(ex)); } finally { if (stream != null) { stream.Close(); stream.Dispose(); stream = null; } } }
internal bool SendPdu(ServiceClass service, MessageType type, string message, DataSet dicom) { bool result = true; if (State == State.Aborted) { throw AbortException; } if (service != null) { service.LastMessage = null; } dicom.Part10Header = false; dicom.TransferSyntaxUID = MessageControl.IsCommand(type) ? Syntax.ImplicitVrLittleEndian : service.Syntaxes[0]; MemoryStream memory = new MemoryStream(); dicom.Write(memory); byte[] bytes = memory.ToArray(); PresentationDataPdu pdu = null; PresentationDataValue pdv = null; int index = 0; int remaining = (int)memory.Length; int size = this.packetSize - 128; State = (State != State.Closing) ? State.Waiting : State; while (remaining > this.packetSize && result) { pdu = new PresentationDataPdu(service.Syntaxes[0]); pdv = new PresentationDataValue(service.PresentationContextId, service.Syntaxes[0], type, bytes, index, size); pdu.Values.Add(pdv); result = Send(message, pdu); index += size; remaining -= size; } if (result) { pdu = new PresentationDataPdu(service.Syntaxes[0]); pdv = new PresentationDataValue(service.PresentationContextId, service.Syntaxes[0], type + 2, bytes, index, remaining); pdu.Values.Add(pdv); result = Send(message, pdu); } return(result); }
private void Shift(string filename) { using (FileStream stream = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read)) { EK.Capture.Dicom.DicomToolKit.DataSet dicom = new EK.Capture.Dicom.DicomToolKit.DataSet(); dicom.Read(stream); stream.Close(); // apply the shift to the image pixels ushort[] pixels = (ushort[])dicom[t.PixelData].Value; for (int n = 0; n < pixels.Length; n++) { pixels[n] = lut[pixels[n]]; } if (AddPixelValuesCheckBox.Checked) { pixels[0] = 0; pixels[1] = (ushort)(maxvalue - 1); } dicom.Set(t.BitsStored, N); dicom.Set(t.HighBit, N - 1); if (AddPixelValuesCheckBox.Checked) { pixels[0] = 0; pixels[1] = (ushort)(maxvalue - 1); dicom.Set(t.SmallestImagePixelValue, (ushort)0); dicom.Set(t.LargestImagePixelValue, (ushort)(maxvalue - 1)); } else { dicom.Set(t.SmallestImagePixelValue, (ushort)(maxvalue / 4)); dicom.Set(t.LargestImagePixelValue, maxvalue - maxvalue / 4); } dicom.Set(t.WindowWidth, maxvalue / 2); dicom.Set(t.WindowCenter, maxvalue / 2); FileInfo info = new FileInfo(filename); String text = filename.Replace(info.Extension, ""); text += "a" + info.Extension; dicom.Write(text); } }
internal bool SendDataPdu(ServiceClass service, string message, DataSet dicom) { bool result = true; dicom.Part10Header = false; dicom.TransferSyntaxUID = service.Syntaxes[0]; MemoryStream memory = new MemoryStream(); dicom.Write(memory); byte[] bytes = memory.ToArray(); PresentationDataPdu pdu = null; PresentationDataValue pdv = null; int index = 0; int remaining = (int)memory.Length; int size = this.packetSize - 128; while (remaining > this.packetSize && result) { pdu = new PresentationDataPdu(service.Syntaxes[0]); pdv = new PresentationDataValue(service.PresentationContextId, service.Syntaxes[0], MessageType.DataSet, bytes, index, size); pdu.Values.Add(pdv); result = SendPdu(service, message, pdu); index += size; remaining -= size; } if (result) { pdu = new PresentationDataPdu(service.Syntaxes[0]); pdv = new PresentationDataValue(service.PresentationContextId, service.Syntaxes[0], MessageType.LastDataSet, bytes, index, remaining); pdu.Values.Add(pdv); result = SendPdu(service, message, pdu); } return(result); }
public void OnData(MessageType control, Message message) { Logging.Log("StorageServiceSCP.OnData"); DataSet dicom = message.Dicom; if (MessageControl.IsCommand(control)) { AffectedSOPInstanceUID = (string)dicom[t.AffectedSOPInstanceUID].Value; MessageId = (ushort)dicom[t.MessageId].Value; } else { dicom.Add(t.GroupLength(2), (ulong)0); dicom.Add(t.FileMetaInformationVersion, new byte[] { 0, 1 }); dicom.Add(t.MediaStorageSOPClassUID, this.SOPClassUId); dicom.Add(t.MediaStorageSOPInstanceUID, this.AffectedSOPInstanceUID); dicom.Add(t.TransferSyntaxUID, this.syntaxes[0]); dicom.Add(t.ImplementationClassUID, "1.2.3.4"); dicom.Add(t.ImplementationVersionName, "not specified"); dicom.Part10Header = true; dicom.TransferSyntaxUID = syntaxes[0]; int status = 0x0000; ImageStoredEventArgs image = new ImageStoredEventArgs(dicom); image.CallingAeTitle = association.CallingAeTitle; image.CallingAeIpAddress = association.CallingAeIpAddress; if (ImageStored != null) { try { ImageStored(this, image); if (image.Cancel) { status = 0xC000; } } catch (Exception ex) { status = 0xA700; Logging.Log(LogLevel.Error, String.Format("Exception caught from ImageStored, {0}", ex.Message)); } } #if DEBUG else { string uid = (string)dicom[t.SOPInstanceUID].Value; dicom.Write(uid + ".dcm"); } #endif DataSet response = new DataSet(); response.Add(t.GroupLength(0), (uint)144); // the number is calculated later anyway response.Add(t.AffectedSOPClassUID, this.SOPClassUId); // response.Add(t.CommandField, (ushort)CommandType.C_STORE_RSP); // response.Add(t.MessageIdBeingRespondedTo, MessageId); response.Add(t.CommandDataSetType, (ushort)DataSetType.DataSetNotPresent); // response.Add(t.Status, status); response.Add(t.AffectedSOPInstanceUID, AffectedSOPInstanceUID); SendCommand("C-STORE-RSP", response); } }
public void OnData(MessageType control, Message message) { Logging.Log("MppsServiceSCP.OnData"); DataSet dicom = message.Dicom; if (MessageControl.IsCommand(control)) { command = (ushort)dicom[t.CommandField].Value; SOPInstanceUID = (command == (ushort)CommandType.N_CREATE_RQ) ? (string)dicom[t.AffectedSOPInstanceUID].Value : (string)dicom[t.RequestedSOPInstanceUID].Value; MessageId = (ushort)dicom[t.MessageId].Value; } else { dicom.Add(t.GroupLength(2), (ulong)0); dicom.Add(t.FileMetaInformationVersion, new byte[] { 0, 1 }); dicom.Add(t.MediaStorageSOPClassUID, this.SOPClassUId); dicom.Add(t.MediaStorageSOPInstanceUID, this.SOPInstanceUID); dicom.Add(t.TransferSyntaxUID, this.syntaxes[0]); dicom.Add(t.ImplementationClassUID, "1.2.3.4"); dicom.Add(t.ImplementationVersionName, "not specified"); dicom.Part10Header = true; dicom.TransferSyntaxUID = syntaxes[0]; MppsEventArgs mpps = new MppsEventArgs(SOPInstanceUID, command, dicom); mpps.CallingAeTitle = association.CallingAeTitle; mpps.CallingAeIpAddress = association.CallingAeIpAddress; if (command == (ushort)CommandType.N_CREATE_RQ && MppsCreate != null) { MppsCreate(this, mpps); } else if (command == (ushort)CommandType.N_SET_RQ && MppsSet != null) { MppsSet(this, mpps); } else { string uid = (string)dicom[t.SOPInstanceUID].Value; dicom.Write(uid + ".dcm"); } PresentationDataValue pdv = new PresentationDataValue(PresentationContextId, Syntaxes[0], MessageType.LastCommand); DataSet fragment = new DataSet(); fragment.Add(t.GroupLength(0), (uint)144); // fragment.Add(t.AffectedSOPClassUID, this.SOPClassUId); // fragment.Add(t.CommandField, (ushort)command | 0x8000); // fragment.Add(t.MessageIdBeingRespondedTo, MessageId); fragment.Add(t.CommandDataSetType, (ushort)DataSetType.DataSetNotPresent); // fragment.Add(t.Status, (int)((mpps.Cancel) ? 0xC000 : 0)); fragment.Add(t.AffectedSOPInstanceUID, SOPInstanceUID); pdv.Dicom = fragment; PresentationDataPdu request = new PresentationDataPdu(syntaxes[0]); request.Values.Add(pdv); SendPdu((command == (ushort)CommandType.N_CREATE_RQ) ? "N-CREATE-RSP" : ">> N-SET-RSP", request); } }
public long Write(System.IO.Stream stream) { return(DataSet.Write(stream)); }