2 using System.Runtime.InteropServices;
5 using System.Collections.Generic;
13 private Texture3D texture3D;
15 public Bounds boundingBox {
private set;
get; }
23 Image firstSlice = seriesInfo.firstSlice;
24 Image lastSlice = seriesInfo.lastSlice;
26 VectorDouble o1 = firstSlice.GetOrigin();
28 throw(
new System.Exception (
"Invalid origins found in first image."));
30 origin =
new Vector3 ((
float)o1 [0], (
float)o1 [1], (
float)o1 [2]);
37 VectorDouble spacing = firstSlice.GetSpacing();
38 if( spacing.Count < 2 )
39 throw(
new System.Exception (
"Invalid pixel spacing found in images."));
40 pixelSpacing =
new Vector2 ((
float)spacing [1], (
float)spacing [0] );
54 Vector2 imgDimensions =
new Vector2 (firstSlice.GetWidth (), firstSlice.GetHeight ());
57 boundingBox =
new Bounds ((corner2 - corner1) / 2 + corner1, (corner2 - corner1));
58 Debug.Log (
"bounding Box: " + boundingBox.center +
" " + boundingBox.size);
64 private void loadVolumeData()
67 VectorString fileNames = seriesInfo.filenames;
70 ImageSeriesReader reader =
new ImageSeriesReader ();
71 reader.SetFileNames (fileNames);
73 Image
image = reader.Execute();
78 texWidth = Mathf.NextPowerOfTwo ((int)image.GetWidth ());
79 texHeight = Mathf.NextPowerOfTwo ((int)image.GetHeight ());
80 texDepth = Mathf.NextPowerOfTwo ((int)image.GetDepth ());
84 Debug.Log (
"Original texture dimensions: " + origTexWidth +
" " + origTexHeight +
" " +
origTexDepth);
85 Debug.Log (
"Texture dimensions: " + texWidth +
" " + texHeight +
" " + texDepth);
86 colors =
new Color32[ texWidth * texHeight * texDepth ];
91 intercept = Int32.Parse( image.GetMetaData(
"0028|1052") );
92 slope = Int32.Parse( image.GetMetaData(
"0028|1053") );
95 Debug.Log (
"Slope: " + slope +
" Intercept: " + intercept);
97 if (
image.GetDimension () != 3)
99 throw(
new System.Exception(
"Cannot load volume: Image needs to be 3D. Dimensions of image: " +
image.GetDimension()));
104 UInt32 min = UInt32.MaxValue;
105 UInt32 max = UInt32.MinValue;
107 Debug.Log (
"Pixel format: " + image.GetPixelID ());
110 if (
image.GetPixelID () == PixelIDValueEnum.sitkUInt16) {
111 IntPtr bufferPtr = image.GetBufferAsUInt16 ();
114 UInt16 *ptr = (UInt16 *)bufferPtr.ToPointer();
116 int consecutiveIndex = 0;
117 for (UInt32 z = 0; z < texDepth; z++) {
118 for (UInt32 y = 0; y < texHeight; y++) {
119 for (UInt32 x = 0; x < texWidth; x++) {
121 long jumpingIndex = x + y * texWidth + z*texWidth*texHeight;
123 UInt32 pixelValue = (UInt32)((UInt16)ptr [consecutiveIndex]);
124 colors [jumpingIndex] =
F2C (pixelValue);
126 if (pixelValue > max)
128 if (pixelValue < min)
131 histogram.addValue (pixelValue);
139 }
else if (
image.GetPixelID() == PixelIDValueEnum.sitkInt16 ) {
140 IntPtr bufferPtr = image.GetBufferAsInt16 ();
143 Int16 *ptr = (Int16 *)bufferPtr.ToPointer();
145 int consecutiveIndex = 0;
146 for (UInt32 z = 0; z < texDepth; z++) {
147 for (UInt32 y = 0; y < texHeight; y++) {
148 for (UInt32 x = 0; x < texWidth; x++) {
150 long jumpingIndex = x + y * texWidth + z*texWidth*texHeight;
152 UInt32 pixelValue = (UInt32)((Int16)ptr[consecutiveIndex] + Int16.MaxValue);
153 colors [jumpingIndex] =
F2C (pixelValue);
155 if (pixelValue > max)
157 if (pixelValue < min)
160 histogram.addValue (pixelValue);
168 }
else if (
image.GetPixelID() == PixelIDValueEnum.sitkInt32 ) {
169 IntPtr bufferPtr = image.GetBufferAsInt32 ();
172 Int32 *ptr = (Int32 *)bufferPtr.ToPointer();
174 int consecutiveIndex = 0;
176 for (UInt32 z = 0; z < texDepth; z++) {
177 for (UInt32 y = 0; y < texHeight; y++) {
178 for (UInt32 x = 0; x < texWidth; x++) {
180 long jumpingIndex = x + y * texWidth + z*texWidth*texHeight;
184 UInt32 pixelValue = (UInt32)((Int32)ptr[consecutiveIndex]) + (UInt32)Int16.MaxValue;
185 colors [jumpingIndex] =
F2C (pixelValue);
187 if (pixelValue > max)
189 if (pixelValue < min)
192 histogram.addValue (pixelValue);
201 throw(
new System.Exception (
"Unsupported pixel format: " +
image.GetPixelID()));
306 seriesInfo.setMinMaxPixelValues (min, max);
309 histogram.setMinMaxPixelValues (min, max);
315 Debug.Log (
"Loaded.");
318 public Texture3D getTexture3D()
320 if( texture3D != null )
325 Debug.Log (
"Generating DICOM texture:");
326 texture3D =
new Texture3D( texWidth, texHeight, texDepth, TextureFormat.ARGB32,
false);
327 texture3D.SetPixels32(
colors);
329 texture3D.wrapMode = TextureWrapMode.Clamp;
331 Debug.Log (
"\tGenerated DICOM texture.");
void setupTransformationMatrices()
static Color32 F2C(UInt32 value)
Vector3 transformPixelToPatientPos(Vector2 pixel, float layer=0)