private void FormMain_Load(object sender, EventArgs e) { Image3D image; //string testPath = @"MANIX\CER-CT\ANGIO CT"; //string testPath = @"BRAINIX\SOUS - 702"; //string testPath = @"BRAINIX\T2W-FE-EPI - 501"; string testPath = @"vtkBrain"; double min, max; using (var reader = new DicomReader(TestData.GetPath(testPath))) { image = reader.ReadImage3D(); min = reader.MinValue; max = reader.MaxValue; } display = new DisplayImage(image.LengthX, image.LengthY); var tf = new TransferFunction1D(); tf.Add(0, Color.Black); tf.Add(max * 0.2, Color.Blue); tf.Add(max * 0.6, Color.Red); tf.Add(max, Color.Yellow); renderer = new TransferFunctionRenderer(image, display, tf); scrollBarSlice.Maximum = image.LengthZ - 1; pictureBoxDisplay.Image = display.GetBitmap(); Render(); }
protected override Image3D OnProcess(Image3D image, Progress progress) { if (cache == null) { if (!CombineFilesToSlices) { using (var reader = new DicomReader(Path)) { cache = reader.ReadImage3D(progress); cache.Minimum = reader.MinValue; cache.Maximum = reader.MaxValue; } } else { using (var reader = new FileToSliceDicomReader(Path)) { cache = reader.ReadImage3D(progress); cache.Minimum = reader.MinValue; cache.Maximum = reader.MaxValue; } } Log.I("Image loaded. min: " + cache.Minimum + "; max: " + cache.Maximum); } return cache; }
public void Read_ValidExplicitVRSequence_YieldsSuccess(byte[] bytes) { var stream = new MemoryStream(bytes); var source = new StreamByteSource(stream); var reader = new DicomReader { IsExplicitVR = true }; var observer = new MockObserver(); var result = reader.Read(source, observer); Assert.Equal(DicomReaderResult.Success, result); }
private static DicomDataset ReadFragment(byte[] bytes, Endian endian, bool explicitVr) { var dataset = new DicomDataset(); var reader = new DicomReader { IsExplicitVR = explicitVr }; var byteSource = new ByteBufferByteSource(new MemoryByteBuffer(bytes)) { Endian = endian }; reader.Read(byteSource, new DicomDatasetReaderObserver(dataset)); return(dataset); }
private void openFolderToolStripMenuItem_Click(object sender, EventArgs e) { if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) { var folder = folderBrowserDialog1.SelectedPath; var enumerator = new DicomFileEnumerator(new DirectoryInfo(folder)); while (enumerator.MoveNext()) { Debug.WriteLine(enumerator.Current.File.Name); report = DicomReader.GetStructuredReport(enumerator.Current.Dataset); ShowAllResults(true); return; } MessageBox.Show("The selected folder does not contain any supported DICOM files.", "Error - Dicom File Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public void Read_ValidExplicitVRData_YieldsSuccess(DicomTag tag, DicomVR vr, string data, byte[] bytes) { var stream = new MemoryStream(bytes); var source = new StreamByteSource(stream); var reader = new DicomReader { IsExplicitVR = true }; var observer = new LastElementObserver(); var result = reader.Read(source, observer); Assert.Equal(DicomReaderResult.Success, result); Assert.Equal(tag, observer.Tag); Assert.Equal(vr, observer.VR); Assert.Equal(data, observer.Data); }
private void openFileToolStripMenuItem_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { var path = openFileDialog1.FileName; var file = DicomFileEnumerator.TryOpen(path); if (file != null) { report = DicomReader.GetStructuredReport(file.Dataset); ShowAllResults(true); } else { MessageBox.Show("The selected file is invalid or in an unsupported format.", "Error - Invalid DICOM File", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
public Image3D ReadImage3D(Progress progress) { this.progress = progress; var directory = new DirectoryInfo(path); var files = directory.GetFiles(); // TODO: remove (allow max 900 slices) if (files.Length > 900) { Array.Resize<FileInfo>(ref files, 900); } // --- Image3D slice0; using (var reader0 = new DicomReader(files[0].FullName)) { slice0 = reader0.ReadImage3D(new Progress()); } this.image = new Image3D(files.Length, slice0.LengthY, slice0.LengthX); this.image.CloneSlice(slice0, 0, 0); MinValue = Math.Min(MinValue, slice0.Minimum); MaxValue = Math.Max(MaxValue, slice0.Maximum); slice0 = null; slicesDone++; //ThreadPool.SetMaxThreads(8, 8); for (int z = 1; z < files.Length; z++) { ReadSlice(new SliceInfo(files[z], z)); //ThreadPool.QueueUserWorkItem(new WaitCallback(ReadSlice), ); } while (slicesDone != image.LengthZ) { Thread.Sleep(50); } progress.Done(); path = null; return image; }
private DicomDataset DeepClone_(DicomDataset dataset) { var ms = new MemoryStream(); var target = new StreamByteTarget(ms); var writer = new DicomWriter(DicomTransferSyntax.ImplicitVRLittleEndian, DicomWriteOptions.Default, target); var walker = new DicomDatasetWalker(dataset); walker.Walk(writer); var clone = new DicomDataset(); var reader = new DicomReader { IsExplicitVR = false }; var byteSource = new ByteBufferByteSource( new MemoryByteBuffer(ms.ToArray())); reader.Read(byteSource, new DicomDatasetReaderObserver(clone)); return(clone); }
private void ProcessPDataTF(object state) { var pdu = (PDataTF)state; try { foreach (var pdv in pdu.PDVs) { if (_dimse == null) { // create stream for receiving command if (_dimseStream == null) { _dimseStream = new MemoryStream(); } } else { // create stream for receiving dataset if (_dimseStream == null) { if (_dimse.Type == DicomCommandField.CStoreRequest) { var pc = Association.PresentationContexts.FirstOrDefault(x => x.ID == pdv.PCID); var file = new DicomFile(); file.FileMetaInfo.MediaStorageSOPClassUID = pc.AbstractSyntax; file.FileMetaInfo.MediaStorageSOPInstanceUID = _dimse.Command.Get <DicomUID>(DicomTag.AffectedSOPInstanceUID); file.FileMetaInfo.TransferSyntax = pc.AcceptedTransferSyntax; file.FileMetaInfo.ImplementationClassUID = Association.RemoteImplemetationClassUID; file.FileMetaInfo.ImplementationVersionName = Association.RemoteImplementationVersion; file.FileMetaInfo.SourceApplicationEntityTitle = Association.CallingAE; _dimseStream = CreateCStoreReceiveStream(file); } else { _dimseStream = new MemoryStream(); } } } _dimseStream.Write(pdv.Value, 0, pdv.Value.Length); if (pdv.IsLastFragment) { if (pdv.IsCommand) { _dimseStream.Seek(0, SeekOrigin.Begin); var command = new DicomDataset(); var reader = new DicomReader(); reader.IsExplicitVR = false; reader.Read(new StreamByteSource(_dimseStream), new DicomDatasetReaderObserver(command)); _dimseStream = null; var type = command.Get <DicomCommandField>(DicomTag.CommandField); switch (type) { case DicomCommandField.CStoreRequest: _dimse = new DicomCStoreRequest(command); break; case DicomCommandField.CStoreResponse: _dimse = new DicomCStoreResponse(command); break; case DicomCommandField.CFindRequest: _dimse = new DicomCFindRequest(command); break; case DicomCommandField.CFindResponse: _dimse = new DicomCFindResponse(command); break; case DicomCommandField.CMoveRequest: _dimse = new DicomCMoveRequest(command); break; case DicomCommandField.CMoveResponse: _dimse = new DicomCMoveResponse(command); break; case DicomCommandField.CEchoRequest: _dimse = new DicomCEchoRequest(command); break; case DicomCommandField.CEchoResponse: _dimse = new DicomCEchoResponse(command); break; case DicomCommandField.NActionRequest: _dimse = new DicomNActionRequest(command); break; case DicomCommandField.NActionResponse: _dimse = new DicomNActionResponse(command); break; case DicomCommandField.NCreateRequest: _dimse = new DicomNCreateRequest(command); break; case DicomCommandField.NCreateResponse: _dimse = new DicomNCreateResponse(command); break; case DicomCommandField.NDeleteRequest: _dimse = new DicomNDeleteRequest(command); break; case DicomCommandField.NDeleteResponse: _dimse = new DicomNDeleteResponse(command); break; case DicomCommandField.NEventReportRequest: _dimse = new DicomNEventReportRequest(command); break; case DicomCommandField.NEventReportResponse: _dimse = new DicomNEventReportResponse(command); break; case DicomCommandField.NGetRequest: _dimse = new DicomNGetRequest(command); break; case DicomCommandField.NGetResponse: _dimse = new DicomNGetResponse(command); break; case DicomCommandField.NSetRequest: _dimse = new DicomNSetRequest(command); break; case DicomCommandField.NSetResponse: _dimse = new DicomNSetResponse(command); break; default: _dimse = new DicomMessage(command); break; } _dimse.PresentationContext = Association.PresentationContexts.FirstOrDefault(x => x.ID == pdv.PCID); if (!_dimse.HasDataset) { if (DicomMessage.IsRequest(_dimse.Type)) { ThreadPool.QueueUserWorkItem(PerformDimseCallback, _dimse); } else { _processQueue.Queue((_dimse as DicomResponse).RequestMessageID, PerformDimseCallback, _dimse); } _dimse = null; return; } } else { if (_dimse.Type != DicomCommandField.CStoreRequest) { _dimseStream.Seek(0, SeekOrigin.Begin); var pc = Association.PresentationContexts.FirstOrDefault(x => x.ID == pdv.PCID); _dimse.Dataset = new DicomDataset(); _dimse.Dataset.InternalTransferSyntax = pc.AcceptedTransferSyntax; var source = new StreamByteSource(_dimseStream); source.Endian = pc.AcceptedTransferSyntax.Endian; var reader = new DicomReader(); reader.IsExplicitVR = pc.AcceptedTransferSyntax.IsExplicitVR; reader.Read(source, new DicomDatasetReaderObserver(_dimse.Dataset)); _dimseStream = null; } else { var request = _dimse as DicomCStoreRequest; try { var dicomFile = GetCStoreDicomFile(); _dimseStream = null; _isTempFile = false; // NOTE: dicomFile will be valid with the default implementation of CreateCStoreReceiveStream() and // GetCStoreDicomFile(), but can be null if a child class overrides either method and changes behavior. // See documentation on CreateCStoreReceiveStream() and GetCStoreDicomFile() for information about why // this might be desired. request.File = dicomFile; if (request.File != null) { request.Dataset = request.File.Dataset; } } catch (Exception e) { var fileName = ""; if (_dimseStream is FileStream) { fileName = (_dimseStream as FileStream).Name; } // failed to parse received DICOM file; send error response instead of aborting connection SendResponse(new DicomCStoreResponse(request, new DicomStatus(DicomStatus.ProcessingFailure, e.Message))); Logger.Error("Error parsing C-Store dataset: " + e.ToString()); (this as IDicomCStoreProvider).OnCStoreRequestException(fileName, e); return; } } if (DicomMessage.IsRequest(_dimse.Type)) { ThreadPool.QueueUserWorkItem(PerformDimseCallback, _dimse); } else { _processQueue.Queue((_dimse as DicomResponse).RequestMessageID, PerformDimseCallback, _dimse); } _dimse = null; } } } } catch (Exception e) { SendAbort(DicomAbortSource.ServiceUser, DicomAbortReason.NotSpecified); Logger.Error("Exception processing P-Data-TF PDU: " + e.ToString()); } finally { SendNextMessage(); } }
private void ProcessPDataTF(PDataTF pdu) { try { foreach (var pdv in pdu.PDVs) { if (_dimse == null) { // create stream for receiving command if (_dimseStream == null) { _dimseStream = new MemoryStream(); } } else { // create stream for receiving dataset if (_dimseStream == null) { if (_dimse.Type == DicomCommandField.CStoreRequest) { var pc = Association.PresentationContexts.FirstOrDefault(x => x.ID == pdv.PCID); var file = new DicomFile(); file.FileMetaInfo.MediaStorageSOPClassUID = pc.AbstractSyntax; file.FileMetaInfo.MediaStorageSOPInstanceUID = _dimse.Command.Get <DicomUID>(DicomTag.AffectedSOPInstanceUID); file.FileMetaInfo.TransferSyntax = pc.AcceptedTransferSyntax; file.FileMetaInfo.ImplementationClassUID = Association.RemoteImplemetationClassUID; file.FileMetaInfo.ImplementationVersionName = Association.RemoteImplementationVersion; file.FileMetaInfo.SourceApplicationEntityTitle = Association.CallingAE; string fileName; if (this is IDicomCStoreProvider) { fileName = (this as IDicomCStoreProvider).GetTempFileName(file.FileMetaInfo.MediaStorageSOPInstanceUID); } else { throw new DicomNetworkException("C-Store SCP not implemented"); } file.Save(fileName); _dimseStream = File.OpenWrite(fileName); _dimseStream.Seek(0, SeekOrigin.End); } else { _dimseStream = new MemoryStream(); } } } _dimseStream.Write(pdv.Value, 0, pdv.Value.Length); if (pdv.IsLastFragment) { if (pdv.IsCommand) { _dimseStream.Seek(0, SeekOrigin.Begin); var command = new DicomDataset(); var reader = new DicomReader(); reader.IsExplicitVR = false; reader.Read(new StreamByteSource(_dimseStream), new DicomDatasetReaderObserver(command)); _dimseStream = null; var type = command.Get <DicomCommandField>(DicomTag.CommandField); switch (type) { case DicomCommandField.CStoreRequest: _dimse = new DicomCStoreRequest(command); break; case DicomCommandField.CStoreResponse: _dimse = new DicomCStoreResponse(command); break; case DicomCommandField.CFindRequest: _dimse = new DicomCFindRequest(command); break; case DicomCommandField.CFindResponse: _dimse = new DicomCFindResponse(command); break; case DicomCommandField.CMoveRequest: _dimse = new DicomCMoveRequest(command); break; case DicomCommandField.CMoveResponse: _dimse = new DicomCMoveResponse(command); break; case DicomCommandField.CEchoRequest: _dimse = new DicomCEchoRequest(command); break; case DicomCommandField.CEchoResponse: _dimse = new DicomCEchoResponse(command); break; default: _dimse = new DicomMessage(command); break; } if (!_dimse.HasDataset) { ThreadPool.QueueUserWorkItem(PerformDimseCallback, _dimse); _dimse = null; return; } } else { if (_dimse.Type != DicomCommandField.CStoreRequest) { _dimseStream.Seek(0, SeekOrigin.Begin); _dimse.Dataset = new DicomDataset(); _dimse.Dataset.InternalTransferSyntax = _dimse.Command.InternalTransferSyntax; var source = new StreamByteSource(_dimseStream); source.Endian = _dimse.Command.InternalTransferSyntax.Endian; var reader = new DicomReader(); reader.IsExplicitVR = _dimse.Command.InternalTransferSyntax.IsExplicitVR; reader.Read(source, new DicomDatasetReaderObserver(_dimse.Dataset)); _dimseStream = null; } else { var fileName = (_dimseStream as FileStream).Name; _dimseStream.Close(); _dimseStream = null; var request = _dimse as DicomCStoreRequest; request.File = DicomFile.Open(fileName); request.File.File.IsTempFile = true; request.Dataset = request.File.Dataset; } ThreadPool.QueueUserWorkItem(PerformDimseCallback, _dimse); _dimse = null; } } } } catch (Exception e) { SendAbort(DicomAbortSource.ServiceUser, DicomAbortReason.NotSpecified); Logger.Log(LogLevel.Error, e.ToString()); } finally { SendNextMessage(); } }
private void ReadSlice(object sliceInfoObj) { if (disposed) { return; } var info = (SliceInfo)sliceInfoObj; Image3D slice; using (var reader = new DicomReader(info.File.FullName)) { slice = reader.ReadImage3D(new Progress()); } var imageCopy = image; // is not null before disposed. if (disposed) { return; } imageCopy.CloneSlice(slice, 0, info.Index); lock (statusLockObject) { MinValue = Math.Min(MinValue, slice.Minimum); MaxValue = Math.Max(MaxValue, slice.Maximum); slicesDone++; } progress.UpdateIncrement((double)1.0 / image.LengthZ); }