/// <summary> /// Fills the target array with 3D data while applying basic preprocessing. /// </summary> /// <param name="groupState">synchronized Threadstate used to observe progress of one or multiple threads.</param> /// <param name="files">all the DICOM files.</param> /// <param name="width">width of a DICOM slice.</param> /// <param name="height">height of a DICOM slice.</param> /// <param name="target">1D array receiving the 3D data.</param> /// <param name="start">Start index used to determine partition of images to be computed</param> /// <param name="end">End index used to determine upper bound of partition of images to be computed</param> private static void PreProcess(ThreadGroupState groupState, IReadOnlyList <DiFile> files, int width, int height, IList <int> target, int start, int end) { var storedBytes = new byte[4]; for (var layer = start; layer < end; ++layer) { var currentDiFile = files[layer]; var pixelData = currentDiFile.RemoveElement(0x7FE0, 0x0010); uint mask = ~(0xFFFFFFFF << (currentDiFile.GetHighBit() + 1)); int allocated = currentDiFile.GetBitsAllocated() / 8; var baseOffset = layer * width * height; using (var pixels = new MemoryStream(pixelData.GetValues())) { for (var y = 0; y < height; ++y) { for (var x = 0; x < width; ++x) { //get current Int value pixels.Read(storedBytes, 0, allocated); var value = BitConverter.ToInt32(storedBytes, 0); var currentPix = GetPixelIntensity((int)(value & mask), currentDiFile); target[baseOffset + y * width + x] = currentPix; } } } groupState.IncrementProgress(); Thread.Sleep(10); } groupState.Done(); }
/// <summary> /// Unity coroutine for loading the selected folder of files. /// </summary> /// <param name="folderPath">Path of the folder containing the DICOM files</param> /// <param name="threadGroupState">Thread safe thread-state used to observe progress of one or multiple threads.</param> /// <returns>IEnumerator for usage as a coroutine</returns> private IEnumerator InitFiles(string folderPath, ThreadGroupState threadGroupState) { threadGroupState.Register(); //string[] filePaths = Directory.GetFiles(folderPath); //filePaths = Array.FindAll(filePaths, HasNoExtension); var fileNames = GetFiles(folderPath); DicomFiles = new DiFile[fileNames.Count]; threadGroupState.TotalProgress = fileNames.Count; yield return(null); var zeroBased = true; foreach (var path in fileNames) { var diFile = new DiFile(); diFile.InitFromFile(path); if (zeroBased && diFile.GetImageNumber() == DicomFiles.Length) { ShiftLeft(DicomFiles); zeroBased = false; } if (zeroBased) { DicomFiles[diFile.GetImageNumber()] = diFile; } else { DicomFiles[diFile.GetImageNumber() - 1] = diFile; } threadGroupState.IncrementProgress(); yield return(null); } Width = DicomFiles[0].GetImageWidth(); Height = DicomFiles[0].GetImageHeight(); _data = new int[DicomFiles.Length * Width * Height]; VolumeTexture = null; WindowCenterPresets = DicomFiles[0].GetElement(0x0028, 0x1050)?.GetDoubles() ?? new[] { double.MinValue }; WindowWidthPresets = DicomFiles[0].GetElement(0x0028, 0x1051)?.GetDoubles() ?? new[] { double.MinValue }; WindowCenter = WindowCenterPresets[0]; WindowWidth = WindowWidthPresets[0]; MinPixelIntensity = (int)(DicomFiles[0].GetElement(0x0028, 0x1052)?.GetDouble() ?? 0d); MaxPixelIntensity = (int)((DicomFiles[0].GetElement(0x0028, 0x1053)?.GetDouble() ?? 1d) * (Math.Pow(2, DicomFiles[0].GetBitsStored()) - 1) + MinPixelIntensity); threadGroupState.Done(); }
/// <summary> /// Fills the target color array with the pixels for all saggital images in range from start to end (excluding end). /// </summary> /// <param name="groupState">synchronized Threadstate used to observe progress of one or multiple threads.</param> /// <param name="processed">synchronized queue which will be filled with each image index, that is ready.</param> /// <param name="data">pixel intensity values in a 3D Array mapped to a 1D Array.</param> /// <param name="width">width of a transversal image.</param> /// <param name="height">height of a transversal image.</param> /// <param name="files">all the DICOM files.</param> /// <param name="target">target jagged array, which the result will be written to.</param> /// <param name="windowWidth">Option to set custom windowWidth, Double.MinValue to not use it</param> /// <param name="windowCenter">Option to set custom windowCenter, Double.MinValue to not use it</param> /// <param name="start">Start index used to determine partition of images to be computed</param> /// <param name="end">End index used to determine upper bound of partition of images to be computed</param> private static void CreateSagTextures(ThreadGroupState groupState, ConcurrentQueue <int> processed, int[] data, int width, int height, IReadOnlyList <DiFile> files, IList <Color32[]> target, double windowWidth, double windowCenter, int start, int end) { for (var x = start; x < end; ++x) { target[x] = new Color32[height * files.Count]; FillPixelsSagittal(x, data, width, height, files, target[x], TransferFunction.Identity, windowWidth, windowCenter); processed.Enqueue(x); Thread.Sleep(5); } groupState.Done(); }
/// <summary> /// Fills the target color array with the pixels for all transversal images in range from start to end (excluding end). /// </summary> /// <param name="groupState">synchronized Threadstate used to observe progress of one or multiple threads.</param> /// <param name="processed">synchronized queue which will be filled with each image index, that is ready.</param> /// <param name="data">pixel intensity values in a 3D Array mapped to a 1D Array.</param> /// <param name="width">width of a transversal image.</param> /// <param name="height">height of a transversal image.</param> /// <param name="files">all the DICOM files.</param> /// <param name="target">target jagged array, which the result will be written to.</param> /// <param name="windowWidth">Option to set custom windowWidth, Double.MinValue to not use it</param> /// <param name="windowCenter">Option to set custom windowCenter, Double.MinValue to not use it</param> /// <param name="start">Start index used to determine partition of images to be computed</param> /// <param name="end">End index used to determine upper bound of partition of images to be computed</param> private static void CreateTransTextures(ThreadGroupState groupState, ConcurrentQueue <int> processed, int[] data, int width, int height, IReadOnlyList <DiFile> files, IList <Color32[]> target, double windowWidth, double windowCenter, int start, int end) { for (var layer = start; layer < end; ++layer) { target[layer] = new Color32[width * height]; FillPixelsTransversal(layer, data, width, height, files, target[layer], TransferFunction.Identity, windowWidth, windowCenter); processed.Enqueue(layer); Thread.Sleep(5); } groupState.Done(); }
/// <summary> /// Fills the given 3D color array using the given 3D pixel intensity array of same size. /// </summary> /// <param name="groupState">synchronized Threadstate used to observe progress of one or multiple threads.</param> /// <param name="data">pixel intensity values in a 3D Array mapped to a 1D Array.</param> /// <param name="dicomFiles">all the DICOM files.</param> /// <param name="width">width of a transversal image.</param> /// <param name="height">height of a transversal image.</param> /// <param name="target">§D color array mapped to 1D Array.</param> /// <param name="windowWidth">Option to set custom windowWidth, Double.MinValue to not use it</param> /// <param name="windowCenter">Option to set custom windowCenter, Double.MinValue to not use it</param> /// <param name="start">Start index used to determine partition of images to be computed</param> /// <param name="end">End index used to determine upper bound of partition of images to be computed</param> private void CreateVolume(ThreadGroupState groupState, IReadOnlyList <int> data, IReadOnlyList <DiFile> dicomFiles, int width, int height, IList <Color32> target, double windowWidth, double windowCenter, int start, int end) { var idx = start * width * height; for (var z = start; z < end; ++z) { var idxPartZ = z * width * height; for (var y = 0; y < height; ++y) { var idxPart = idxPartZ + y * width; for (var x = 0; x < width; ++x, ++idx) { target[idx] = TransferFunction.DYN_ALPHA(GetRGBValue(data[idxPart + x], dicomFiles[z], windowWidth, windowCenter)); } } Thread.Sleep(5); groupState.IncrementProgress(); } groupState.Done(); }