2 using System.Runtime.InteropServices;
5 using System.Collections.Generic;
11 public int slice {
private set;
get; }
18 private Texture2D texture2D;
33 slice = Mathf.Clamp (
slice, 0, seriesInfo.filenames.Count - 1);
36 VectorString fileNames = seriesInfo.filenames;
39 image = SimpleITK.ReadImage( fileNames[
slice] );
40 loadImageData (
image);
42 VectorDouble o1 = image.GetOrigin();
44 throw(
new System.Exception (
"Invalid origins found in first image."));
46 origin =
new Vector3 ((
float)o1 [0], (
float)o1 [1], (
float)o1 [2]);
51 VectorDouble direction = image.GetDirection();
52 if( direction.Count < 6 )
53 throw(
new System.Exception (
"Invalid direction cosines found in images."));
54 directionCosineX =
new Vector3 ((
float)direction [0], (
float)direction [3], (
float)direction [6]);
55 directionCosineY =
new Vector3 ((
float)direction [1], (
float)direction [4], (
float)direction [7]);
61 float absX = Mathf.Abs (sliceNormal.x);
62 float absY = Mathf.Abs (sliceNormal.y);
63 float absZ = Mathf.Abs (sliceNormal.z);
64 if (absX > absY && absX > absZ) {
66 }
else if (absY > absX && absY > absZ) {
68 }
else if (absZ > absX && absZ > absY) {
79 VectorDouble spacing = image.GetSpacing();
80 if( spacing.Count < 2 )
81 throw(
new System.Exception (
"Invalid pixel spacing found in images."));
82 pixelSpacing =
new Vector2 ((
float)spacing [1], (
float)spacing [0] );
92 private void loadImageData( Image
image )
98 texWidth = Mathf.NextPowerOfTwo ((int)image.GetWidth ());
99 texHeight = Mathf.NextPowerOfTwo ((int)image.GetHeight ());
104 colors =
new Color32[ texWidth * texHeight ];
114 if (image.GetDimension () != 2 && image.GetDimension () != 3)
116 throw(
new System.Exception(
"Only 2D and 3D images are currently supported. Dimensions of image: " + image.GetDimension()));
119 Debug.Log (
"DICOM Pixel format: " + image.GetPixelID ());
121 UInt32 min = UInt32.MaxValue;
122 UInt32 max = UInt32.MinValue;
125 if (image.GetPixelID () == PixelIDValueEnum.sitkUInt16) {
126 IntPtr bufferPtr = image.GetBufferAsUInt16 ();
129 UInt16 *ptr = (UInt16 *)bufferPtr.ToPointer();
131 int consecutiveIndex = 0;
133 for (UInt32 y = 0; y < texHeight; y++) {
134 for (UInt32 x = 0; x < texWidth; x++) {
135 if (x < origTexWidth && y < origTexHeight) {
136 long jumpingIndex = x + y * texWidth;
138 UInt32 pixelValue = (UInt32)((UInt16)ptr[consecutiveIndex]);
139 colors [jumpingIndex] =
F2C (pixelValue);
141 if (pixelValue > max)
143 if (pixelValue < min)
151 }
else if ( image.GetPixelID() == PixelIDValueEnum.sitkInt16 ) {
152 IntPtr bufferPtr = image.GetBufferAsInt16 ();
155 Int16 *ptr = (Int16 *)bufferPtr.ToPointer();
157 int consecutiveIndex = 0;
159 for (UInt32 y = 0; y < texHeight; y++) {
160 for (UInt32 x = 0; x < texWidth; x++) {
161 if (x < origTexWidth && y < origTexHeight) {
162 long jumpingIndex = x + y * texWidth;
164 UInt32 pixelValue = (UInt32)((Int16)ptr[consecutiveIndex] + Int16.MaxValue);
165 colors [jumpingIndex] =
F2C (pixelValue);
167 if (pixelValue > max)
169 if (pixelValue < min)
177 }
else if ( image.GetPixelID() == PixelIDValueEnum.sitkInt32 ) {
178 IntPtr bufferPtr = image.GetBufferAsInt32 ();
181 Int32 *ptr = (Int32 *)bufferPtr.ToPointer();
183 int consecutiveIndex = 0;
185 for (UInt32 y = 0; y < texHeight; y++) {
186 for (UInt32 x = 0; x < texWidth; x++) {
187 if (x < origTexWidth && y < origTexHeight) {
188 long jumpingIndex = x + y * texWidth;
192 UInt32 pixelValue = (UInt32)((Int32)ptr[consecutiveIndex]) + (UInt32)Int16.MaxValue;
193 colors [jumpingIndex] =
F2C (pixelValue);
195 if (pixelValue > max)
197 if (pixelValue < min)
206 throw(
new System.Exception (
"Unsupported pixel format: " + image.GetPixelID()));
209 seriesInfo.setMinMaxPixelValues (min, max);
219 if (texture2D != null)
223 throw(
new System.Exception (
"Trying to get Texture2D from a DICOM which has " +
dimensions +
" dimensions!"));
226 texture2D =
new Texture2D (texWidth, texHeight, TextureFormat.ARGB32,
false,
true);
227 texture2D.SetPixels32 (
colors);
void setupTransformationMatrices()
static Color32 F2C(UInt32 value)
SliceOrientation sliceOrientation